package com.postracker.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/ajax")
public class Ajax {

	/**
	 * Handles and retrieves the AJAX Add page
	 */
	@RequestMapping(value = "addPage", method = RequestMethod.GET)
	public String getAjaxAddPage() {
		return "ajax/addPage";
	}

	/**
	 * Handles request for adding two numbers
	 */
	@RequestMapping(value = "addOperation", method = RequestMethod.POST)
	public @ResponseBody
	Integer add(
			@RequestParam(value = "inputNumber1", required = true) Integer inputNumber1,
			@RequestParam(value = "inputNumber2", required = true) Integer inputNumber2) {

		// Delegate to service to do the actual adding
		Integer sum = Integer.valueOf(inputNumber1.intValue()
				+ inputNumber2.intValue());
		// @ResponseBody will automatically convert the returned value into JSON
		// format
		// You must have Jackson in your classpath
		return sum;
	}
}